<?php
namespace app\mall\controller;

use app\data\controller\api\Auth;
use app\data\model\DataUser;
use app\mall\logic\uploadLogic;
use app\mall\logic\userLogic;
use app\mall\logic\walletLogic;
use app\mall_back\model\GoodsLuckyDrawModel;
use app\mall_back\model\TescoCommentModel;
use app\mall_back\model\TescoRecordModel;
use app\mall_back\model\UserWalletDetailedModel;
use PHPQRCode\QRcode;
use think\db\Query;
use think\facade\Db;

class User extends Auth{

    //个人中心
    public function individual_center(){
        $logic = new userLogic();
        $data = $logic->individual_center($this->uuid);
        $this->success('个人中心',$data);
    }

    //修改密码
    public function change_password(){
        $old_password = request()->param('old_password','');
        $new_password = request()->param('new_password','');
        $new_re_password = request()->param('new_re_password','');
        $logic = new userLogic();
        $res = $logic->change_password($this->uuid,$old_password,$new_password,$new_re_password);
        if ($res['code'] == 200) $this->success($res['msg']);
        $this->error($res['msg'],'',$res['code']);
    }

    //重置密码
    public function reset_password(){
        $code = request()->param('code',0);
        $new_password = request()->param('new_password','');
        $new_re_password = request()->param('new_re_password','');
        $logic = new userLogic();
        $res = $logic->reset_password($this->user,$new_password,$new_re_password,$code);
        if ($res['code'] == 200) $this->success($res['msg']);
        $this->error($res['msg'],'',$res['code']);
    }

    //重置支付密码
    public function reset_pay_password(){
        $code = request()->param('code',0);
        $new_pay_password = request()->param('new_pay_password','');
        $new_re_pay_password = request()->param('new_re_pay_password','');
        $logic = new userLogic();
        $res = $logic->reset_pay_password($this->user,$new_pay_password,$new_re_pay_password,$code);
        if ($res['code'] == 200) $this->success($res['msg']);
        $this->error($res['msg'],'',$res['code']);
    }
    //上传文件
    public function upload(){
        $Upload = new uploadLogic();
        $res = $Upload->file();
        if ($res['code'] == 200) $this->success($res['msg'],['url'=>$res['url']]);
        $this->error($res['msg'],'',$res['code']);
    }

    //修改用户资料
    public function update_info(){
        $headimg = request()->param('headimg','');
        $nickname = request()->param('nickname','');
        $logic = new userLogic();
        $res = $logic->update_info($this->uuid,$headimg,$nickname);
        if ($res['code'] == 200) $this->success($res['msg']);
        $this->error($res['msg'],'',$res['code']);
    }

    //更换绑定手机号
    public function change_phone(){
        $code = request()->param('code',0);
        $phone = request()->param('phone','');
        $logic = new userLogic();
        $res = $logic->change_phone($this->uuid,$phone,$code);
        if ($res['code'] == 200) $this->success($res['msg']);
        $this->error($res['msg'],'',$res['code']);
    }
    /*
     * 生成用户二维码
     * */
    public function qrcode()
    {
        $user = $this->user; //二维码内容
        $logic = new userLogic();
        $res = $logic->scerweima1($user['phone']);
        if ($res['code'] == 200) $this->success('用户二维码',$res['data']);
        $this->error($res['msg'],'',$res['code']);
    }

    /**
     * 签到送积分
     */
    public function signIn(){
        $uid = $this->uuid;
        $giveIntegral = sysdata('signIn_give_integral');
        $dayStartTime = date('Y-m-d 00:00:00');
        $is = UserWalletDetailedModel::where(['u_id'=>$uid,'type'=>20])
            ->where('create_time','>',$dayStartTime)
            ->find();
        if($is){
            $this->error('今天已经签过到了');
        }

        // 启动事务
        Db::startTrans();
        try {
            $res1 = walletLogic::wallet_detailed_add($uid, $giveIntegral, 20, 2, 0, 'add','签到送积分');//增加记录
            $res2 = Db::table('one_user_wallet')
                ->where('u_id', '=', $uid)
                ->inc('integral', $giveIntegral)
                ->update();//增加积分
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }

        if($res1 && $res2){
          $this->success('签到成功');
        }
        $this->error('签到失败');

    }


    /**
     * 我邀请的用户
     */
    public function mySubUser(){
        $user = $this->user;
        $limit = request()->param('limit');
        $search_uid = request()->get('search_uid/d',0);
        $query = DataUser::where(['pid1'=>$user['id'],'deleted'=>0])
            ->field('id,phone,headimg,username,nickname')
            ->with(['user_wallet']);
        if($search_uid){
            $query->where('id',$search_uid);
        }
        $list =  $query->paginate($limit);
        $this->success('获取成功',$list);
    }

    /**
     * 查看其他用户资料
     */
    public function otherUserInfo()
    {
        $otherUid = request()->param('other_uid');
        $row_type = request()->get('row_type');
        $limit    = request()->get('limit');
        $result   = ['userInfo' => [], 'list' => []];

        $userInfo           = $list = DataUser::field('id,headimg,username,nickname')->find($otherUid);
        $result['userInfo'] = $userInfo;

        if ($row_type == 0) { //夺宝记录
            $drawIds = TescoRecordModel::where(['u_id' => $otherUid])
                ->fieldRaw('distinct goods_lucky_draw_id')
                ->column('goods_lucky_draw_id');

            $data = GoodsLuckyDrawModel::with(['goods'])
                ->where('id', 'IN', $drawIds)
                ->paginate($limit);

            $items = $data->items();
            foreach ($items as $k => $v) {
                $items[$k]['lucky_numbers'] = TescoRecordModel::where('u_id', $otherUid)
                    ->where('goods_lucky_draw_id', $v['id'])
                    ->column('lucky_number');
                $items[$k]['user'] = $userInfo;
            }

            $tmp = json_decode(json_encode($data));
            $tmp->data = $items;
            $result['list'] = $tmp;
        } elseif ($row_type == 1) { //中奖记录
            $data           = TescoRecordModel::where(['u_id' => $otherUid, 'is_win' => 1])
                ->with(['goodsluckydraw' => ['goods', 'user']])
                ->paginate($limit);
            $result['list'] = $data;
        } elseif ($row_type == 2) { //晒单记录
            $TescoCommentModel = new TescoCommentModel();
            $list              = $TescoCommentModel->with(['commentUser', 'goodsLuckyDraw.goods'])
                ->where(['u_id' => $otherUid])
                ->group('lucky_draw_id')
                ->order(['id' => 'desc'])
                ->paginate($limit);
            if ($list) {
                foreach ($list as $key => $value) {
                    $list[$key]['images_arr'] = explode('|', $value['images']);
                }
            }
            $result['list'] = $list;
        }

        $this->success('获取成功', $result);
    }

    /**
     * 我的夺宝币变动记录
     */
    public function moneyChangeLog(){
        $limit = $this->request->get('limit',10);
        $list = UserWalletDetailedModel::where(['u_id'=>$this->uuid])->append(['type_text'])
            ->paginate();
        return $this->success('获取成功',$list);
    }



}